<?php
//session_start();
require_once("initializeData.php");
date_default_timezone_set(TIMEZONE);  

class DB
{
	public $localhost;
	public $username;
	public $password;
	public $db;
	public $conn;	
	
public function DB($localhost=DB_HOST, $password=DB_PASSWORD, $username=USERNAME, $db=DB_NAME)
{
	$this->localhost=$localhost;
	$this->password=$password;
	$this->username=$username;
	$this->db=$db;
	$this->open();
}

public function open()
{
	$this->conn=mysql_connect($this->localhost,$this->username,$this->password);
	mysql_select_db($this->db);
	mysql_query("SET CHARACTER SET utf8");
}

public function close()
{
	mysql_close($this->conn);
}


public function addStaff()
{
	$sql = "INSERT INTO staffinfo (name, password, position, email, image, sex, level) 
		VALUES ('".$_POST['name']."', '".$_POST['password']."', '".$_POST['position']."','".$_POST['email']."','".$_POST['image']."','".$_POST['gender']."','".$_POST['level']."')";
        
        mysql_query($sql,$this->conn);
		$id = mysql_insert_id($this->conn);
		return $id;
}

public function getUserinfo($id,$pass)
{
    //echo "$name $pass";
    $sql = "SELECT * FROM staffinfo where id = '$id' and password = '$pass'";
    $result = mysql_query($sql,$this->conn);
    return $result;
}

//luo
public function getUser($id,$pass,$level)
    {
		//echo "$id $pass $level";
        if($level=='manager')
		{
			$sql = "SELECT * FROM staffinfo where id = '$id' and password = '$pass' and level='$level'";
		    $result = mysql_query($sql,$this->conn);
		    if (mysql_fetch_row($result)) {
		 	return 1;
		}
		return 0;
		}
		else
		{
			$sql = "SELECT * FROM staffinfo where id = '$id' and password = '$pass' and level = '$level'";
		$result = mysql_query($sql,$this->conn);
		
           if (mysql_fetch_row($result)){ 
		 	return 1;
		   }
        
            else 
			{  
		    return 0;
            }
		}
	}
    public function getNameFromId($id)
    {
        $sql = "SELECT * FROM staffinfo where id = '$id'";
		$result = mysql_query($sql,$this->conn);
		if(mysql_fetch_array($result)){
			$name = mysql_fetch_array($result);
			return $name;
		}
		else return 1;
		
    }
	
	//leit
	public function getData()
	{
	$sql = "SELECT * FROM staffinfo where level='staff' order by id limit 100";
	$result = mysql_query($sql, $this->conn);
	return $result;
}
    
    public function getDataAll()
	{
	$sql = "SELECT * FROM staffinfo where order by id limit 100";
	$result = mysql_query($sql, $this->conn);
	return $result;
}

//dj
public function getStaffInfo($arr)
{   
	$id=$arr[0];
	$name=$arr[1];
	$sql="SELECT * FROM staffinfo where id='$id'and name='$name'";
	$result = mysql_query($sql,$this->conn);
	$row=mysql_fetch_array($result);
	return $row;
}

//luo
public function getUserstate($name,$id)
{
    //echo "$name $id";
  $time = time();
  $date = getdate($time);
  $year = $date["year"];
  $mon = $date["mon"];
  $day = $date["mday"];
    $sql = "SELECT * FROM qiandaoinfo where name = '$name' and id = '$id' and year = '$year' and month = '$mon' and date = '$day'";
    $s = mysql_query($sql,$this->conn);
    if($b = mysql_fetch_array($s)){
   
    //echo "$b['start'] $b['finish']";
       if($b['finish']==NULL){
           return $b['start'];
       }
       else return $b['finish'];
    
    }
    else return "NULL";
    
}

//dj
public function getState($arr)
{
	$id=$arr[0];
	$name=$arr[1];
	$time = time();
    $date = getdate($time);
	$year = $date["year"];
	$mon = $date["mon"];		
	$day = $date["mday"];
	$sql="SELECT * FROM qiandaoinfo where id='$id'and name='$name' and year='$year' and month='$mon' and date='$day'";
	$s=mysql_query($sql,$this->conn);
	if($row=mysql_fetch_array($s))
	{
		//$row=mysql_fetch_array($s);
		if($row['finish']==NULL)
		{
			return $row['start'];
		}
		else
			return $row['finish'];
	}
	else
	{
		$a="Null";
		return $a;
	}
}

//leit
public function checkAttendance($name, $id)
{
	$time = time();
	$date = getdate($time);
	$year = $date["year"];
	$mon = $date["mon"];
	$day = $date["mday"];
	$sql = "SELECT * FROM qiandaoinfo where id = '$id' and year = '$year' and month = '$mon' and date = '$day'";
	$result = mysql_query($sql, $this->conn);
	$row = mysql_fetch_array($result);
	
	if($_POST['d'] == "work" && $row['start'] == null){
		
		$sql="insert into qiandaoinfo(id, name, year, month, date, start) value ('$id', '$name', '$year', '$mon', '$day', 'work')";
		mysql_query($sql, $this->conn);
		$_SESSION['start'] = "work";
		/*add some data in attendance_statistics table*/
		$sql="insert into attendance_statistics(id, year, month, name, num_present, num_offday) value('$id', '$year', '$mon', '$name', 0, 0)";
		mysql_query($sql, $this->conn);
		
		$sql = "SELECT * FROM attendance_statistics where id = '$id' and year = '$year' and month = '$mon'";
		$result1 = mysql_query($sql, $this->conn);
		$row1 = mysql_fetch_array($result1);
		
		$num_present = $row1['num_present'];
		$num_present = $num_present + 1;
		echo $num_present;
		$sql="update attendance_statistics set num_present = '$num_present' where id = '$id' and name = '$name' and year = '$year' and month = '$mon'";
		mysql_query($sql, $this->conn);
		
	}
	else if($_POST['d'] == "offday"  && $row['start'] == null){
		
		$sql="insert into qiandaoinfo(id, name, year, month, date, start)value ('$id', '$name', '$year', '$mon', '$day', 'offday')";
		mysql_query($sql, $this->conn);
		/*add some data in attendance_statistics table*/
		$sql="insert into attendance_statistics(id, year, month, name, num_present, num_offday) value('$id', '$year', '$mon', '$name', 0, 0)";
		mysql_query($sql, $this->conn);
		
		$sql = "SELECT * FROM attendance_statistics where id = '$id' and year = '$year' and month = '$mon'";
		$result1 = mysql_query($sql, $this->conn);
		$row1 = mysql_fetch_array($result1);
		
		$num_offday = $row1['num_offday'];
		$num_offday = $num_offday + 1;
		echo $num_offday;
		$sql="update attendance_statistics set num_offday = '$num_offday' where id = '$id' and name = '$name' and year = '$year' and month = '$mon'";
		mysql_query($sql, $this->conn);
		
	}
	else if($_POST['d'] == "xiaban" && $row['start'] == "work"){
		
		$sql="update qiandaoinfo set finish = 'xiaban' where id = '$id' and year = '$year' and month = '$mon' and date = '$day'";
		mysql_query($sql, $this->conn);
		
	}
}

public function getAttendancein($name,$id,$year,$month)
{
    $sql = "SELECT * FROM attendance_statistics where id = '$id' and year = '$year' and month = '$month' ";
    $result = mysql_query($sql, $this->conn);
    if($value = mysql_fetch_array($result)){
    return $value['num_present'];
    }
    else return 0;
}
    
public function getAttendancefo($name,$id,$year,$month)
{
    $sql = "SELECT * FROM attendance_statistics where id = '$id' and year = '$year' and month = '$month' ";
    $result = mysql_query($sql,$this->conn);
    if($value = mysql_fetch_array($result)){
    
    //echo "$value";
    return $value['num_offday'];
    }
    else return 0;
}

//dj
public function sendAssignment($id,$sname,$mname,$detail)
{
	$sql="select * from staffinfo where id = '$id' and name='$sname' ";
    $s=mysql_query($sql,$this->conn);
    if(mysql_fetch_array($s)){
        $date = date("Y-m-d h:i:s",time());
        $sql="insert into taskinfo(id,sname,mname,date,content) values('$id','$sname','$mname','$date','$detail')";
        mysql_query($sql,$this->conn);
        return 0;
    }
    else return 1;
}


//dj
public function getAssignment($name,$id)
{
	$sql="select * from taskinfo where sname='$name' and id='$id' and state='0' order by date desc limit 10";
	$s=mysql_query($sql,$this->conn);
	return $s;
}
    
public function getAllAssignment($name){
    $sql = "SELECT * FROM taskinfo where mname = '$name' order by date desc limit 10";
    $result = mysql_query($sql, $this->conn);
    return $result;
}

//dj
public function haveAssignment($name,$id)
{
	$sql="select * from taskinfo where sname='$name' and id='$id' and state='0'";
	$s=mysql_query($sql,$this->conn);
	if(mysql_fetch_array($s))
		return 1;
	else
		return 0;
}

//dj
public function finishAssignment($time,$name,$id)
{
	$sql="update taskinfo set state='finish' where date='$time' and sname='$name' and id='$id'";
	mysql_query($sql,$this->conn);
}

//dj
public function fireStaffFromStaff($id)
{
	$sql="delete from staffinfo where id='$id'";
	mysql_query($sql,$this->conn);
}

//dj
public function FireStaffFromBirth($id)
{
	$sql="delete from birthinfo where id='$id'";
	mysql_query($sql,$this->conn);
}

public function modify($id){
    $sql = "UPDATE staffinfo SET name = '".$_POST['name']."', level = '".$_POST['level']."', position = '".$_POST['position']."' WHERE id = '$id' ";
    mysql_query($sql, $this->conn);
    $sql = "UPDATE qiandaoinfo SET name = '".$_POST['name']."' WHERE id = '$id' ";
    mysql_query($sql, $this->conn);
    $sql = "UPDATE attendance_statistics SET name = '".$_POST['name']."' WHERE id = '$id' ";
    mysql_query($sql, $this->conn);
    
}

//luo
public function addStaffbirth($id,$age)
{
    $sql = "INSERT INTO birthinfo (id, name, year, month, date,age) 
		VALUES ('$id','".$_POST['name']."', '".$_POST['year']."', '".$_POST['month']."','".$_POST['date']."','$age')";
    mysql_query($sql, $this->conn);
}

//luo
public function updatebirth($age,$id)
{
    $sql = "UPDATE birthinfo SET age = '$age' WHERE id = '$id' ";
    mysql_query($sql, $this->conn);
}

//luo
public function getUserbirth($name,$id)
{
    $sql = "SELECT * FROM birthinfo where name = '$name' and id = '$id'";
    $result = mysql_query($sql,$this->conn);
    
    return $result;
}

public function addBulletin()
    {
         //$time = time();
         //$date = getdate($time);
        $date = date("Y-m-d h:i:s",time());
        $sql = "INSERT INTO bulletininfo (date, content)
        VALUES ('$date','".$_POST['detail']."')";
        if(mysql_query($sql,$this->conn)){
            //echo"success";
            return 1;
        }
        else return 0;
    }
	
	//luo
public function getBulletin()
{
    $sql = "SELECT * FROM bulletininfo order by date desc limit 10";
    $result = mysql_query($sql, $this->conn);
    return $result;
}

public function getbirth($arr)
{
	$id=$arr[0];
	$name=$arr[1];
	$sql = "SELECT * FROM birthinfo where name = '$name' and id = '$id'";
    $row = mysql_query($sql,$this->conn);
	$result=mysql_fetch_array($row);
	return $result;
}

public function ranUser()
{
	$ran1='abccefghijklmnopqrstuvwxyz';
	$sex='mf';
	for($a=0;$a<10000;$a++)
	{
		$name='';
		$level='';
		$gender='';
		for($i=0;$i<6;$i++)
		{
			$name .=$ran1[mt_rand(0,strlen($ran1)-1)];
		}
		$gender=$sex[mt_rand(0,strlen($sex)-1)];
		$sql="insert into staffinfo(name,password,position,email,image,sex,level) VALUES('$name','123','program','12345@qq.com','upload/timg.jpg','$gender','staff') ";
		mysql_query($sql,$this->conn);
	}
}

public function getStaffInfo2($id)
{
	$sql="SELECT * FROM staffinfo where id='$id'";
	$result = mysql_query($sql,$this->conn);
	if($row=mysql_fetch_array($result))
	{
		return $row;
	}
	else
		return 0;
}

public function getState2($id)
{
	$time = time();
    $date = getdate($time);
	$year = $date["year"];
	$mon = $date["mon"];		
	$day = $date["mday"];
	$sql="SELECT * FROM qiandaoinfo where id='$id'and year='$year' and month='$mon' and date='$day'";
	$s=mysql_query($sql,$this->conn);
	if($row=mysql_fetch_array($s))
	{
		//$row=mysql_fetch_array($s);
		if($row['finish']==NULL)
		{
			return $row['start'];
		}
		else
			return $row['finish'];
	}
	else
	{
		$a="Null";
		return $a;
	}
}

public function getbirth2($id)
{
	$sql = "SELECT * FROM birthinfo where id = '$id'";
    $row = mysql_query($sql,$this->conn);
	$result=mysql_fetch_array($row);
	return $result;
}

public function checkIn($name,$id)
{
	$time = time();
    $date = getdate($time);
	$year = $date["year"];
	$mon = $date["mon"];		
	$day = $date["mday"];
	$sql="SELECT * FROM qiandaoinfo where id='$id'and year='$year' and month='$mon' and date='$day'";
	$s=mysql_query($sql,$this->conn);
	if($row=mysql_fetch_array($s))
	{
		//$row=mysql_fetch_array($s);
		if($row['start']==NULL)
		{
			return 1;
		}
		else
			return 0;
	}
	else
	{
		return 1;
	}
}




}



?>